package com.twu.biblioteca;

import java.util.ArrayList;
import java.util.List;

public class Registry {
    List<Book> booksInLibrary = new ArrayList<Book>();
    List<Book> borrowedBooks = new ArrayList<Book>();

    public Registry() {
        booksInLibrary.add(new Book("123", "LOTR", "Tolkien", "Classic"));
        booksInLibrary.add(new Book("234", "HP", "Rowling", "HPPub"));
    }

    public String bookDisplay(){
        String dispBooks = "";
        for(int i=0;i< booksInLibrary.size();i++)
             dispBooks+= "ISBN =" + booksInLibrary.get(i).ISBN + "Title =" + booksInLibrary.get(i).title + "Author = " + booksInLibrary.get(i).author + "Publisher = " + booksInLibrary.get(i).publisher + "\n";
        return dispBooks;
    }

    public boolean checkOutBook(String title){
        Book bookToBeCheckedOut = searchBook(title, booksInLibrary);
        if(bookToBeCheckedOut!=null){
            borrowedBooks.add(bookToBeCheckedOut);
            booksInLibrary.remove(bookToBeCheckedOut);
            return true;
        }
        return false;
           }

    public boolean returnBook(String title){
        Book returnedBook = searchBook(title,borrowedBooks);
        if(borrowedBooks.contains(returnedBook)){
            borrowedBooks.remove(returnedBook);

            booksInLibrary.add(returnedBook);
        return true;
        }
        return false;
    }

    public Book searchBook(String bookTitleToBeSearched, List<Book> listOfBooksToSearch){
       for(Book book: listOfBooksToSearch)
        if (bookTitleToBeSearched.equals(book.title))
            return book;
        return null;
    }
}

